package com.elitedatai.mchtest.control.controller;

import com.dji.sdk.common.HttpResultResponse;
import com.elitedatai.mchtest.control.model.enums.DroneAuthorityEnum;
import com.elitedatai.mchtest.control.model.enums.RemoteDebugMethodEnum;
import com.elitedatai.mchtest.control.model.param.*;
import com.elitedatai.mchtest.control.service.IControlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;


@RestController
@Slf4j
@RequestMapping("${url.control.prefix}${url.control.version}/devices")
public class DockController {

    @Autowired
    private IControlService controlService;

    /**
     * 创建控制任务，通过远程调试命令控制机场设备
     * @param sn 设备序列号
     * @param serviceIdentifier 服务标识符
     * @param param 远程调试参数
     * @return HTTP响应结果
     */
    @PostMapping("/{sn}/jobs/{service_identifier}")
    public HttpResultResponse createControlJob(@PathVariable String sn,
                                               @PathVariable("service_identifier") String serviceIdentifier,
                                               @Valid @RequestBody(required = false) RemoteDebugParam param) {
        return controlService.controlDockDebug(sn, RemoteDebugMethodEnum.find(serviceIdentifier), param);
    }

    /**
     * 控制无人机飞往指定目标点
     * @param sn 设备序列号
     * @param param 飞往目标点的参数
     * @return HTTP响应结果
     */
    @PostMapping("/{sn}/jobs/fly-to-point")
    public HttpResultResponse flyToPoint(@PathVariable String sn, @Valid @RequestBody FlyToPointParam param) {
        return controlService.flyToPoint(sn, param);
    }

    /**
     * 停止飞往目标点的任务
     * @param sn 设备序列号
     * @return HTTP响应结果
     */
    @DeleteMapping("/{sn}/jobs/fly-to-point")
    public HttpResultResponse flyToPointStop(@PathVariable String sn) {
        return controlService.flyToPointStop(sn);
    }

    /**
     * 控制无人机起飞并飞往指定点
     * @param sn 设备序列号
     * @param param 起飞并飞往指定点的参数
     * @return HTTP响应结果
     */
    @PostMapping("/{sn}/jobs/takeoff-to-point")
    public HttpResultResponse takeoffToPoint(@PathVariable String sn, @Valid @RequestBody TakeoffToPointParam param) {
        return controlService.takeoffToPoint(sn, param);
    }

    /**
     * 获取无人机的飞行控制权限
     * @param sn 设备序列号
     * @return HTTP响应结果
     */
    @PostMapping("/{sn}/authority/flight")
    public HttpResultResponse seizeFlightAuthority(@PathVariable String sn) {
        return controlService.seizeAuthority(sn, DroneAuthorityEnum.FLIGHT, null);
    }

    /**
     * 获取无人机负载设备的控制权限
     * @param sn 设备序列号
     * @param param 无人机负载参数
     * @return HTTP响应结果
     */
    @PostMapping("/{sn}/authority/payload")
    public HttpResultResponse seizePayloadAuthority(@PathVariable String sn, @Valid @RequestBody DronePayloadParam param) {
        return controlService.seizeAuthority(sn, DroneAuthorityEnum.PAYLOAD, param);
    }

    /**
     * 发送负载设备控制命令
     * @param sn 设备序列号
     * @param param 负载设备控制命令参数
     * @return HTTP响应结果
     * @throws Exception 异常信息
     */
    @PostMapping("/{sn}/payload/commands")
    public HttpResultResponse payloadCommands(@PathVariable String sn, @Valid @RequestBody PayloadCommandsParam param) throws Exception {
        param.setSn(sn);
        return controlService.payloadCommands(param);
    }


}